<?php
/**
 * VariableIteracionForm class.
 * VariableIteracionForm is the data structure for keeping
 * the form data related to adding equipos and variables to an iteracion. 
 * It is used by the 'ConfigurarIteracion' action of 'IteracionController'.
 */
class VariableIteracionForm extends CFormModel
{
	
	public $iteracion;
	public $equipo; 
	public $variable;
        public $valor;
	private $_equipo;
        private $_variable;
	
	/**
	 * Declares the validation rules.
	**/
	public function rules()
	{
		return array(
			// equipo, variable and valor are required
			array('equipo, variable', 'required'),
			array('variable', 'verify'),
                );
	}
 
	
	/**
	 * Authenticates the existence of the user in the system.
	 * If valid, it will also make the association between the user, role and project
	 * This is the 'verify' validator as declared in rules().
	 */
	public function verify($attribute,$params)
	{
		if(!$this->hasErrors())  // we only want to authenticate when no other input errors are present
		{
			$equipo = Equipo::model()->findByPk($this->equipo);
                        /*if($equipo===null)
                            $this->addError('equipo','PAILINES'.$this->equipo);*/
                        $variable = Variable::model()->findByPk($this->variable);
                        
                        if($this->iteracion->isEquipoVariableinIteracion($equipo->id, $variable->id))
                        {
                            $this->addError('equipo','This combination of equipo and variable has already been added to the iteracion.'); 
                        }
			else
			{
				$this->_equipo = $equipo;
                                $this->_variable = $variable;
			}
		}
	}
	
	public function assign()
	{
		if($this->_equipo instanceof Equipo && $this->_variable instanceof Variable)
		{
			
			//assign the user, in the specified role, to the project
			//$this->project->assignUser($this->_user->id, $this->rol);  
                        $this->iteracion->assignEquipoVariable($this->_equipo->id, $this->_variable->id);
			//add the association, along with the RBAC biz rule, to our RBAC hierarchy
                        //$auth = Yii::app()->authManager; 
			/*$bizRule='return isset($params["proyecto"]) && $params["proyecto"]->allowCurrentUser("'.$this->rol.'");';  
			$auth->assign($this->rol,$this->_user->id, $bizRule);*/
			return true;
		}
		else
		{
			$this->addError('equipo','Error when attempting to assign this equipo and variable to the iteracion.'); 
			return false;
		}
		
	}
	
	
}
